package com.cbs.android.module.userkit.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.cbs.android.component.application.activity.CBSActivity;
import com.cbs.android.component.applicationutils.network.ErrorResponse;
import com.cbs.android.component.log.L;
import com.cbs.android.component.toast.Toast;
import com.cbs.android.module.user.user.UserModule;
import com.cbs.android.module.user.user.entity.User;
import com.cbs.android.module.user.user.handler.ModuleHandler;
import com.cbs.android.module.userkit.R;
import com.cbs.android.module.userkit.UserKit;
import com.cbs.android.widget.loadingframelayout.LoadingFrameLayout;
import com.google.gson.Gson;

/**
 * Created by broche on 10/30/15.
 */
public class ModifyPasswordActivity extends CBSActivity implements View.OnClickListener {
    private static final int PasswordLength = 6;
    private static final String TAG = ModifyPasswordActivity.class.getName();

    private EditText oldPasswordView;
    private EditText newPasswordView;
    private EditText newPasswordView2;
    private LoadingFrameLayout loadingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED);

        super.setContentView(R.layout.cbs_userkit_activity_modifypassword);

        oldPasswordView = (EditText) findViewById(R.id.cbs_userkit_modifypassword_oldpassword);
        newPasswordView = (EditText) findViewById(R.id.cbs_userkit_modifypassword_newpassword);
        newPasswordView2 = (EditText) findViewById(R.id.cbs_userkit_modifypassword_newpassword2);
        loadingView = (LoadingFrameLayout) findViewById(R.id.cbs_userkit_modifypassword_loading);

        findViewById(R.id.cbs_userkit_modifypassword_back).setOnClickListener(this);
        findViewById(R.id.cbs_userkit_modifypassword_submit).setOnClickListener(this);

        User user = UserModule.getUser();
        if (user == null) {
            Intent intent = new Intent();
            intent.putExtra(UserKit.IntentMessage, getString(R.string.cbs_userkit_verify_unregistered));
            setResult(RESULT_CANCELED, intent);
            finish();
            return;
        }

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.cbs_userkit_modifypassword_back) {
            finish();
        } else if (v.getId() == R.id.cbs_userkit_modifypassword_submit) {
            String pwd1 = newPasswordView.getText().toString();
            String pwd2 = newPasswordView2.getText().toString();
            if (!pwd1.equals(pwd2)) {
                Toast.show(getString(R.string.cbs_userkit_verify_differentpassword));
                return;
            } else if (pwd1.equals("") || pwd2.equals("")) {
                Toast.show(getString(R.string.cbs_userkit_verify_emptypassword));
                return;
            } else if (pwd1.length() < PasswordLength || pwd2.length() < PasswordLength) {
                Toast.show(getString(R.string.cbs_userkit_verify_shortpassword));
                return;
            }

            loadingView.startLoading();
            UserModule.modifyPassword(oldPasswordView.getText().toString(), newPasswordView.getText().toString(), new ModuleHandler<User>() {
                @Override
                public void onSuccess(User entity) {
                    setResult(RESULT_OK);
                    Toast.show(getString(R.string.cbs_userkit_hint_modifySuccess));
                    finish();
                }

                @Override
                public void onFailure(int code, String message) {
                    loadingView.stopLoading();
                    try {
                        ErrorResponse errorResponse = new Gson().fromJson(message, ErrorResponse.class);
                        Toast.show(errorResponse.getErrorMsg());
                    } catch (Exception e) {
                        // TODO what to do
                    }
                    L.w(TAG, "code: " + code + "\tmessage: " + message);
                }

                @Override
                public void onException(Exception exception) {
                    loadingView.stopLoading();
                    L.w(TAG, "", exception);
                }
            });
        }
    }
}
